package com.zlt.bkparking.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zlt.bkparking.dto.ParkingOrderDTO;
import com.zlt.bkparking.dto.VipTypeDTO;
import com.zlt.bkparking.entity.*;
import com.zlt.bkparking.mapper.*;
import com.zlt.bkparking.service.ParkingVIPService;
import com.zlt.bkparking.utils.StringUtil;
import com.zlt.bkparking.vo.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname ParkingVIPServiceImpl
 * @Description TODO
 * @Date 2022/10/4 20:10
 * @Created by ${Lijijun}
 */
@Service
public class ParkingVIPServiceImpl implements ParkingVIPService {

    @Autowired
    private ParkingLotMapper parkingLotMapper;

    @Autowired
    private VipTypeMapper vipTypeMapper;

    @Autowired
    private UserVipMapper userVipMapper;

    @Autowired
    private ParkingOrderMapper parkingOrderMapper;

    @Autowired
    private AppUserMapper appUserMapper;

    @Override
    public List<VipTypeVO> allParkingVipTypes() {
        //获取当前停车场管理员的账户 bu_id =1
        ActiveBackUser subject = (ActiveBackUser) SecurityUtils.getSubject().getPrincipal();
        Long buId = subject.getBuId();
//        System.out.println("buId============>>>>>>>>"+buId);

        ParkingLotExample parkingLotExample = new ParkingLotExample();
        parkingLotExample.createCriteria().andBuIdEqualTo(buId).andPlStateEqualTo((byte)0);
        List<ParkingLot> parkingLots = parkingLotMapper.selectByExample(parkingLotExample);
        if (parkingLots==null||parkingLots.isEmpty())
            throw new RuntimeException("您没有管理的停车场");
        Integer plId = parkingLots.get(0).getPlId();
        VipTypeExample vipTypeExample = new VipTypeExample();
        vipTypeExample.createCriteria().andPlIdEqualTo(plId).andVtStateEqualTo((byte)1);
        List<VipType> vipTypeList = vipTypeMapper.selectByExample(vipTypeExample);
        if (vipTypeList==null ||vipTypeList.isEmpty())
            throw new RuntimeException("您的停车场还没有vip类型信息");
        List<VipTypeVO> vipTypeVOList = new ArrayList<>();
        for (VipType vipType : vipTypeList) {
            VipTypeVO vipTypeVO = new VipTypeVO();
            vipTypeVOList.add(vipTypeVO);
            BeanUtils.copyProperties(vipType,vipTypeVO);
        }
        return vipTypeVOList;
    }

    @Override
    public Boolean insertPKVipType(VipTypeDTO vipTypeDTO) {
        VipType vipType = new VipType();
        BeanUtils.copyProperties(vipTypeDTO,vipType);
        if (!StringUtil.isNotNull(vipType.getVtName()))
            throw  new RuntimeException("VIP类型名不能为空");
        if (vipType.getVtPrice()==null)
            throw new RuntimeException("VIP单价不能为空");
        if (vipType.getVtDay()==null)
            throw new RuntimeException("VIP单价对应的天数不能为空");
        //获取当前停车场管理员的账号 bu_id   1
        ActiveBackUser subject = (ActiveBackUser) SecurityUtils.getSubject().getPrincipal();
        Long buId = subject.getBuId();
        ParkingLotExample parkingLotExample = new ParkingLotExample();
        parkingLotExample.createCriteria().andBuIdEqualTo(buId).andPlStateEqualTo((byte)0);
        List<ParkingLot> parkingLots = parkingLotMapper.selectByExample(parkingLotExample);
        if (parkingLots==null||parkingLots.isEmpty())
            throw new RuntimeException("您没有管理的停车场");
        Integer plId = parkingLots.get(0).getPlId();
        vipType.setPlId(plId);
        vipType.setVtState((byte)1);
        int i = vipTypeMapper.insertSelective(vipType);
        return i>0;
    }

    @Override
    public PageInfo<ParkingOrderVO> selectParkingOrders(ParkingOrderDTO parkingOrderDTO) {
        Integer page = parkingOrderDTO.getPage();
        if (page==null)
            page=1;
        Integer limit = parkingOrderDTO.getLimit();
        if (limit==null)
            limit=5;
        //获取当前停车场管理员的账户 bu_id 1
        ActiveBackUser subject = (ActiveBackUser) SecurityUtils.getSubject().getPrincipal();
        Long buId = subject.getBuId();
        ParkingLotExample parkingLotExample = new ParkingLotExample();
        parkingLotExample.createCriteria().andBuIdEqualTo(buId).andPlStateEqualTo((byte)0);
        List<ParkingLot> parkingLots = parkingLotMapper.selectByExample(parkingLotExample);
        if (parkingLots==null||parkingLots.isEmpty())
            throw new RuntimeException("您没有管理的停车场");
        Integer plId = parkingLots.get(0).getPlId();

        VipTypeExample vipTypeExample = new VipTypeExample();
        vipTypeExample.createCriteria().andPlIdEqualTo(plId);
        vipTypeExample.createCriteria().andVtStateEqualTo((byte)0);
        List<VipType> vipTypeList = vipTypeMapper.selectByExample(vipTypeExample);
        if (vipTypeList==null ||vipTypeList.isEmpty())
            throw new RuntimeException("当前停车场还没有VIP信息");
        List<Integer> vipTypeIds = new ArrayList<>();
        vipTypeIds.add(Integer.MAX_VALUE);
        for (VipType vipType : vipTypeList) {
                vipTypeIds.add(vipType.getVtId());
        }

        UserVipExample userVipExample = new UserVipExample();
        UserVipExample.Criteria criteria = userVipExample.createCriteria();
        criteria.andVtIdIn(vipTypeIds);

        List<UserVip> userVips = userVipMapper.selectByExample(userVipExample);
        if (userVips==null||userVips.isEmpty())
            throw new RuntimeException("没有用户充值当前停车场的VIP");

        List<String> appUserIds =new ArrayList<>();
        appUserIds.add("");
        for (UserVip userVip : userVips)
            appUserIds.add(userVip.getAuId());

        ParkingOrderExample parkingOrderExample = new ParkingOrderExample();
        parkingOrderExample.createCriteria().andAuIdIn(appUserIds).andPlIdEqualTo(plId);
        PageHelper.startPage(page,limit);
        List<ParkingOrder> parkingOrders = parkingOrderMapper.selectByExample(parkingOrderExample);
        PageInfo<ParkingOrderVO> parkingOrderVOPageInfo =new PageInfo(parkingOrders);
        List<ParkingOrderVO> parkingOrderVOList = new ArrayList<>();
        parkingOrderVOPageInfo.setList(parkingOrderVOList);
        if (parkingOrders!=null && !parkingOrders.isEmpty()){
            for (ParkingOrder parkingOrder : parkingOrders) {
                ParkingOrderVO parkingOrderVO = new ParkingOrderVO();
                parkingOrderVOList.add(parkingOrderVO);
                BeanUtils.copyProperties(parkingOrder,parkingOrderVO);
                ParkingLotVO parkingLotVO = new ParkingLotVO();
                parkingOrderVO.setParkingLotVO(parkingLotVO);
                BeanUtils.copyProperties(parkingLots.get(0),parkingLotVO);
                AppUser appUser = appUserMapper.selectByPrimaryKey(parkingOrder.getAuId());
                AppUserVO appUserVO = new AppUserVO();
                parkingOrderVO.setAppUserVO(appUserVO);
                BeanUtils.copyProperties(appUser,appUserVO);
            }
        }
        return parkingOrderVOPageInfo;
    }









    @Override
    public PageInfo<ParkingOrderVO> selectParkingOrdersByAuId(ParkingOrderDTO parkingOrderDTO) {
        Integer page = parkingOrderDTO.getPage();
        if (page==null)
            page=1;
        Integer limit = parkingOrderDTO.getLimit();
        if (limit==null)
            limit=5;
        //获取当前停车场管理员的账户 bu_id 1
        ActiveBackUser subject = (ActiveBackUser) SecurityUtils.getSubject().getPrincipal();
        Long buId = subject.getBuId();
        ParkingLotExample parkingLotExample = new ParkingLotExample();
        parkingLotExample.createCriteria().andBuIdEqualTo(buId).andPlStateEqualTo((byte)0);
        List<ParkingLot> parkingLots = parkingLotMapper.selectByExample(parkingLotExample);
        if (parkingLots==null||parkingLots.isEmpty())
            throw new RuntimeException("您没有管理的停车场");
        Integer plId = parkingLots.get(0).getPlId();

        VipTypeExample vipTypeExample = new VipTypeExample();
        vipTypeExample.createCriteria().andPlIdEqualTo(plId);
        vipTypeExample.createCriteria().andVtStateEqualTo((byte)0);
        List<VipType> vipTypeList = vipTypeMapper.selectByExample(vipTypeExample);
        if (vipTypeList==null ||vipTypeList.isEmpty())
            throw new RuntimeException("当前停车场还没有VIP信息");
        List<Integer> vipTypeIds = new ArrayList<>();
        vipTypeIds.add(Integer.MAX_VALUE);
        for (VipType vipType : vipTypeList) {
            vipTypeIds.add(vipType.getVtId());
        }
        /////////
        String auUsername = parkingOrderDTO.getAuUsername();
        AppUserExample appUserExample = new AppUserExample();
        appUserExample.createCriteria().andAuUsernameLike("%"+auUsername+"%");
        List<AppUser> appUserList = appUserMapper.selectByExample(appUserExample);
        List<String> appUserIds =new ArrayList<>();
        appUserIds.add("");
        if (appUserList!=null && !appUserList.isEmpty()){
            for (AppUser appUser : appUserList) {
                    appUserIds.add(appUser.getAuId());
            }
        }
        ////////////////////////////////////
        //查询uservip

        UserVipExample userVipExample = new UserVipExample();
        userVipExample.createCriteria().andVtIdIn(vipTypeIds).andAuIdIn(appUserIds);
        List<UserVip> userVips = userVipMapper.selectByExample(userVipExample);
        if (userVips==null||userVips.isEmpty()){
            throw new RuntimeException("该停车场没有该VIP用户的订单记录");
        }
        List<String> appUserIdss =new ArrayList<>();
        appUserIds.add("");
        for (UserVip userVip : userVips) {
            appUserIdss.add(userVip.getAuId());

        }
        ParkingOrderExample parkingOrderExample = new ParkingOrderExample();
        parkingOrderExample.createCriteria().andAuIdIn(appUserIdss).andPlIdEqualTo(plId);
        PageHelper.startPage(page,limit);
        List<ParkingOrder> parkingOrders = parkingOrderMapper.selectByExample(parkingOrderExample);
        PageInfo<ParkingOrderVO> parkingOrderVOPageInfo =new PageInfo(parkingOrders);
        List<ParkingOrderVO> parkingOrderVOList = new ArrayList<>();
        parkingOrderVOPageInfo.setList(parkingOrderVOList);
        if (parkingOrders!=null && !parkingOrders.isEmpty()){
            for (ParkingOrder parkingOrder : parkingOrders) {
                ParkingOrderVO parkingOrderVO = new ParkingOrderVO();
                parkingOrderVOList.add(parkingOrderVO);
                BeanUtils.copyProperties(parkingOrder,parkingOrderVO);
                ParkingLotVO parkingLotVO = new ParkingLotVO();
                parkingOrderVO.setParkingLotVO(parkingLotVO);
                BeanUtils.copyProperties(parkingLots.get(0),parkingLotVO);
                AppUser appUser = appUserMapper.selectByPrimaryKey(parkingOrder.getAuId());
                AppUserVO appUserVO = new AppUserVO();
                parkingOrderVO.setAppUserVO(appUserVO);
                BeanUtils.copyProperties(appUser,appUserVO);
            }
        }
        return parkingOrderVOPageInfo;
    }
}
